package com.zjdiepu.www.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.zjdiepu.www.base.enums.type.FFmpegOutPutType;
import com.zjdiepu.www.common.util.FileUtil;

/**
 * 项目名称 dp-test <br/> 
 * 类名称 MediaCompresser <br/>  
 * 类描述 视频/音频压缩（损失质量）<br/>    
 * 创建人 xiongxiaotun <br/> 
 * 创建时间 2017年4月15日 上午11:21:18  <br/>   
 * 修改人 xiongxiaotun <br/> 
 * 修改时间 2017年4月15日 上午11:21:18   <br/>  
 * 修改备注   <br/> 
 * @version V1.0   <br/>
 */
public class MediaCompresser {
	private static final String ffmpeg_home = "D:\\ffmpeg.exe";
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title process  <br/>
	 * @Description 生成多媒体文件 <br/>
	 * @param arguments
	 * @param ffmpegOutPutType
	 * @return
	 * @version V1.0  <br/>
	 */
	public static boolean process(FFmpegOutPutType ffmpegOutPutType, String... arguments) {
		if(null == ffmpegOutPutType) {
			return false;
		}
		List<String> command = buildCommand(ffmpegOutPutType, arguments);
		if(null == command || command.size() == 0) {
			return false;
		}
		StringBuilder sb  = new StringBuilder();
		for (int i = 0; i < command.size(); i++)
			sb.append(command.get(i) + " ");
		Process videoProcess = null;
		try {
			videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
			InputStream inputStream = videoProcess.getInputStream();
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
						String line = null;
						while((line = reader.readLine()) != null){
							System.out.println(line);
						}
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						FileUtil.release(inputStream, null);
					}
				}
			}).start();
			videoProcess.waitFor();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(null != videoProcess) {
				videoProcess.destroy();
			}
		}
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title buildCommandForImage  <br/>
	 * @Description 构建命令 <br/>
	 * 				【0】输入文件【1】输出文件【2】抓取的时间【3】抓取时长【4】图片宽度【5】图片高度
	 * @param ffmpegOutPutType 输出文件格式
	 * @param arguments
	 * @return
	 * @version V1.0  <br/>
	 */
	public static List<String> buildCommand(FFmpegOutPutType ffmpegOutPutType, String... arguments){
		String inputFilePath = arguments[0];
		String outputFilePath = arguments[1];
		if(StringUtils.isBlank(inputFilePath) || StringUtils.isBlank(outputFilePath)) {
			return null;
		}
		File inputFile = new File(inputFilePath);
		if (!inputFile.exists() || !inputFile.isFile()) {
			return null;
		}
		File outputFile = new File(outputFilePath);
		if (outputFile.exists()) {
			outputFile.delete();
		}
		List<String> command = new ArrayList<String>();
		command.add(ffmpeg_home);
		command.add("-i");
		command.add(inputFilePath);
		command.add("-y");
		switch (ffmpegOutPutType) {
		case MP4:
			command.add("-vcodec");
			command.add("h264");
			command.add("-acodec");
			command.add("aac");
			command.add("-r");
			command.add("15");
			command.add("-fs");
			command.add("10M");
			command.add("-s");
			command.add("1920*1080");
			break;
		case FLV:
			command.add("-ab");  
	        command.add("56");  
	        command.add("-ar");  
	        command.add("22050");  
	        command.add("-qscale");  
	        command.add("8");  
	        command.add("-r");  
	        command.add("15");  
	        command.add("-s");  
	        command.add(arguments[4] + "x" + arguments[5]); 
		case JPG:
			command.add("-f");
			command.add("image2");
			command.add("-ss");
			command.add(arguments[2]);
			command.add("-t");
			command.add("0.001");
			command.add("-s");
			command.add(arguments[4] + "x" + arguments[5]);
		default:
			break;
		}
		command.add(outputFilePath);
		return command;
	}
	
	public static void main(String[] args) {
//		process(FFmpegOutPutType.JPG, "D:\\video\\01_什么是3g.avi", "D:\\outvideo\\test.jpg", "30", null, "800", "600");
		process(FFmpegOutPutType.MP4, "D:\\test2.mp4", "D:\\test2_out.mp4");
	}
}
